查看原文
其他

R绘图模板——散点+分组+size+显著性!!!

王志山 科研后花园 2023-09-08
 

点击上方

“科研后花园”

关注我们

灵感来源:

代码如下:1、设置工作环境:
rm(list = ls())setwd("D:/桌面/散点+分组+size+显著性")
#加载R包library(ggplot2) # Create Elegant Data Visualisations Using the Grammar of Graphicslibrary(rstatix) # Pipe-Friendly Framework for Basic Statistical Tests2、加载数据:
#加载绘图数据(随机编写,无实际意义)df <- read.table("data.txt", header = 1, check.names = F)df$group1 <- factor(df$group1, levels = c("One", "Two", "Three"))df$group2 <- factor(df$group2, levels = c("1", "2", "3","4", "5", "6"))df$group3 <- factor(df$group3, levels = c("A", "B"))

3、计算组间显著性:
#计算显著性p <- df[df$group1 == "Two", ] %>% wilcox_test(Score ~ group3) %>% adjust_pvalue()p
# # A tibble: 1 × 8# .y. group1 group2 n1 n2 statistic p p.adj# <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl># 1 Score A B 13 9 113 0.000271 0.0002714、绘图:
#绘图ggplot(df)+ #散点图 geom_point(aes(x = group2, y = Score, color = group3, size = Count))+ #设置分面 facet_grid(~group1, switch='x')+#switch='x'可将标签位置由顶部移至底部,当switch='y'时,可将位于右边的分面标签移至左边 #主题设置 theme_bw()+ theme(axis.text.x = element_blank(), axis.text.y = element_text(color = "black", size = 12), axis.title = element_text(color = "black", size = 14), axis.ticks.x = element_blank(), strip.background = element_blank(), strip.text = element_text(color = "black", size = 12), panel.grid = element_blank())+ #调整size范围 scale_size_continuous(range = c(2, 7), breaks = c(1,3,6))+ #颜色 scale_color_manual(values = c("#ff4e00","#01cd74"))+ #y轴范围及刻度设置 scale_y_continuous(breaks = c(0,1,2,3,4,5,6,7))+ #图例 guides(color=guide_legend(override.aes = list(size=4,alpha=1)))+ labs(x = NULL, color = NULL)+ #手动添加显著性 geom_segment(df[df$group1 == "Two", ], mapping = aes(x = 2, xend = 5, y = 7.5, yend = 7.5), color = "black", linewidth = 0.6)+ geom_text(df[df$group1 == "Two", ], mapping = aes(x = 3.5, y = 8, label = 'p < 0.001'), size =4.5, color="black")

PS: 以上内容是小编个人学习代码笔记分享,仅供参考学习,欢迎大家一起交流学习。


完整代码(附带注释)及原始数据获取:

夸克网盘(手机端下载夸克APP进行搜索提取):

链接:https://pan.quark.cn/s/d1d866ef85d6

提取码:buxZ


更多推荐

R可视化——基于MicrobiotaProcess包进行物种差异分析!

R绘图模板——气泡图+注释+配对连线!!!

R绘图模板——世界地图的绘制及拓展!!!

基于MicrobiotaProcess包进行PCoA分析!

跟着Nature学绘图——柱状图+散点+配对连线+显著性!!!

R绘图模板——中国地图+散点+柱状图!!!

R绘图模板——热图和柱状堆积图组合图!!!

扩增子测序数据分析还不会?小编整理的全套R语言代码助您轻松解决问题!

R绘图模板源代码获取!

不会用代码做RDA分析?没关系,Canoco软件帮你实现!

R绘图模板——热图+网络图展示mantel test相关性!!!

R绘图模板——箱线图+点线图+显著性+分组!!!

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存